<template>
  <!-- 患者治疗 -->
  <section class="patient-layout">
    <div>
      <patientBasicInfo :cureId="cureId" :patientId="patientId"></patientBasicInfo>
    </div>
    <div v-if="dataInfo">
      <table border="0" width="100%">
        <tr>
          <td>
            <h3 style="margin:0;">治疗项目</h3>
          </td>
          <td style="text-align:right;">
            <el-button @click="$router.push('curecheck')">
              <i class="el-icon-arrow-left"></i>
              <span>返回</span>
            </el-button>
          </td>
        </tr>
      </table>
      <br>
      <table width="100%" border="0" style="table-layout: fixed;">
        <tr>
          <td>开单医生：{{dataInfo.doctorName}}</td>
          <td style="text-align:center;">开单时间：{{dataInfo.time}}</td>
          <td style="text-align:right;">所属科室：{{dataInfo.deptName}}</td>
        </tr>
      </table>
      <br>
      <el-alert type="warning" :closable="false">
        <table slot="title" width="100%" border="0">
          <tr>
            <td>过敏史：</td>
            <td>
              <div v-if="guoMinArr.length==0">否认</div>
              <div v-else>
                <span v-for="(item,index) in guoMinArr" :key="index" style="margin-right:10px;">{{item.allergens}},{{item.allergicReaction}},{{item.allergicDegree}}</span>
              </div>
            </td>
          </tr>
        </table>
      </el-alert>
      <br>
      <table class="table-box">
        <thead>
          <tr>
            <th></th>
            <th>项目</th>
            <th>单位</th>
            <th>单价</th>
            <th v-if="cureStatus!=2">本次治疗</th>
            <th v-if="cureStatus!=2">治疗人员</th>
            <th>总次数</th>
            <th>已治疗次数</th>
            <th class="hide-md ">说明</th>
            <th>备注</th>
            <th>收费状态</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item,index) in dataInfo.cureAdviceVOS" :key="index">
            <td>
              <el-checkbox v-model="item.check"></el-checkbox>
            </td>
            <td>
              {{item.name}}
            </td>
            <td>{{item.unit|changeAssist(unitEnum)}}</td>
            <td>￥{{item.unitPrice}}</td>
            <td v-if="cureStatus!=2">
              <el-input-number style="width:120px;" v-model="item.curNum" :min="1" :max="item.totalCount"></el-input-number>
            </td>
            <td v-if="cureStatus!=2">
              <el-select v-model="item.cureUserId" clearable @clear="getUser" filterable remote :remote-method="getUser">
                <el-option v-for="(item,index) in userArray" :key="index" :label="item.memberName" :value="item.memberId"></el-option>
              </el-select>
            </td>
            <td>{{item.totalCount}}</td>
            <td>
              <el-link type="primary" @click="openUserRecord(item)">{{item.cureCount}}</el-link>
            </td>
            <td class="hide-md ">
              <div style="max-width:300px;">{{item.explain}}</div>
            </td>
            <td>
              <el-input :disabled="cureStatus==2" v-model="item.remark"></el-input>
            </td>
            <td>
              <span v-if="item.payStatus==1" class="success-color">已收费</span>
              <span v-else class="error-color">未收费</span>
            </td>
          </tr>
        </tbody>
      </table>
      <br>
      <div class="form-tool">
        <div>
          <el-button type='primary' :loading="btnLoading" @click="onSubmit">保存</el-button>
        </div>
        <div>
          <el-button @click="onPrint" :loading="btnLoading"> <i class="iconfont icondayin vertical-middle"></i> <span class="vertical-middle">打印治疗单</span> </el-button>
        </div>
      </div>
    </div>
    <cureCheckRecord v-model="dialogRecord" :curItem="curItem"></cureCheckRecord>
    <iframe ref="iframe1" frameborder="0" height="0px" width="0px" src="/print/cure.html"></iframe>
  </section>
</template>
<script>
export default {
  components: {
    patientBasicInfo: () => import("@/pages/_com/patientBasicInfo.vue"),
    cureCheckRecord: () => import("./_com/cureCheckRecord.vue")
  },
  data() {
    return {
      qxBtns: {},
      cureId: "",
      patientId: "",
      cureStatus: "",
      dataInfo: null,
      unitEnum: [],
      guoMinArr: [],
      userArray: [],
      curItem: null,
      dialogRecord: false,
      btnLoading: false
    };
  },
  created() {
    let th = this;
    let query = th.$route.query;
    let fn = async function() {
      th.unitEnum = await th.getCommonConfigByKey("单位");
      th.getData();
    };
    th.verifyUrlAttack(query).then(res => {
      th.cureId = th.jieMi(query.cureId);
      th.patientId = th.jieMi(query.patientId);
      th.cureStatus = query.cureStatus;
      th.getQX();
      th.getDataBi();
      fn();
    });
  },
  mounted() {
    let th = this;
    th.getUser(th.adminUser.memberName);
  },
  methods: {
    async onPrint() {
      let th = this;
      let arr = [];
      let json = {
        hospital: {
          name: th.adminUser.hospitalName,
          logo: th.adminUser.printLogo
        },
        professionalDeptName: th.dataInfo.professionalDeptName,
        doctorName: th.dataInfo.doctorName,
        patientInfo: null,
        time: th.dataInfo.time,
        list: []
      };
      th.dataInfo.cureAdviceVOS.forEach(item => {
        // console.log(item);
        let j = {
          name: item.name,
          sellMoney: 0,
          unit: th.getUnit(item.unit, th.unitEnum),
          adviceNum: item.totalCount,
          price: item.unitPrice ? Number.parseFloat(item.unitPrice) : 0,
          remark: item.remark,
          cureCount:item.cureCount,
        };
        if (j.price && j.adviceNum) {
          j.sellMoney = j.price * j.adviceNum;
        }
        if (item.check) json.list.push(j);
      });
      if (json.list.length == 0) {
        th.$message.warning("请勾选数据");
        return;
      }
      let j = {
        patientId: th.patientId,
        cureId: th.cureId
      };
      th.btnLoading = true;
      //获取患者信息
      let res1 = await th.$api.getPatientDetail(j);
      th.btnLoading = false;
      if (!res1.executed) {
        return;
      }
      json.patientInfo = res1.data.patientInfo ? res1.data.patientInfo : {};
      //诊断
      json.firstVisit = res1.data.firstVisit;
      th.$refs["iframe1"].contentWindow.postMessage(
        json,
        `${window.location.origin}/print/cure.html`
      );
    },
    //打开治疗记录
    openUserRecord(obj) {
      let th = this;
      if (!obj.adviceJournalVOS) return;
      th.curItem = obj;
      th.dialogRecord = true;
    },
    onSubmit() {
      let th = this;
      if (th.cureStatus != 1) {
        th.$message.warning("只能治疗未完成的项目");
        return;
      }
      if (!th.dataInfo.cureAdviceVOS) return;
      let list = [];
      let isUser = true;
      th.dataInfo.cureAdviceVOS.forEach(d => {
        let j = {
          cureAdviceId: d.id,
          currentCureCount: d.curNum,
          cureUserId: d.cureUserId,
          remark: d.remark
        };
        if (d.check && d.totalCount != d.cureCount) {
          //判断是否选择治疗人员
          if (!d.cureUserId) {
            isUser = false;
          }
          list.push(j);
        }
      });
      if (list.length == 0) {
        th.$message.warning("请勾选数据");
        return;
      }
      if (!isUser) {
        th.$message.warning("请选择治疗人员");
        return;
      }
      th.$confirm("确定已经治疗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          fn();
        })
        .catch(() => {});
      const fn = function() {
        let j = {
          confirmCure: JSON.stringify(list)
        };
        th.btnLoading = true;
        th.$api
          .cureAdviceconfirmCure(j)
          .then(res => {
            if (res.executed) {
              th.$message.success("保存成功");
              th.getData();
            } else {
              th.$message.error(res.message);
            }
            th.btnLoading = false;
          })
          .catch(() => {
            th.btnLoading = false;
          });
      };
    },
    getUser(name) {
      let th = this;
      let j = {
        systemCode: th.$root.systemCode,
        hpId: th.adminUser.hpId,
        memberName: name,
        pageNum: 1,
        pageSize:50,
        status: 1
      };
      th.$api.getMemberList(j).then(res => {
        if (res.executed) {
          th.userArray = res.dataBody.list;
        }
      });
    },
    getData() {
      let th = this;
      let j = {
        cureId: th.cureId,
        cureStatus: th.cureStatus
      };
      th.$api.cureAdvicelistCureProgram(j).then(res => {
        if (res.executed) {
          if (res.data.cureAdviceVOS) {
            res.data.cureAdviceVOS.forEach(el => {
              el.curNum = 1;
              el.check = true;
            });
          }
          th.dataInfo = res.data;
        }
      });
    },
    getUnit(id, arr) {
      let th = this;
      let str = "";
      let data = arr.find(d => {
        return d.id == id;
      });
      if (data) {
        str = data.desc;
      }
      return str;
    },
    //获取病历
    getDataBi() {
      let th = this;
      let j = {
        cureId: th.cureId
      };
      th.$api.getCureMedicalDetailId(j).then(res => {
        if (res.executed) {
          // //初步诊断
          // cureFirstVisitRecord: null,
          // //病历信息
          // cureMedical: null,
          // //过敏史
          // curePatientAllergy: null,
          // //既往，个人，疫苗
          // curePatientHistory: null
          let d = res.data.curePatientAllergy;
          if (d && res.data.curePatientAllergy.length > 0) th.guoMinArr = d;
        }
      });
    }
  }
};
</script>
